package com.rongke.sms;

import com.rongke.utils.RandomUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.net.URLEncoder;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/3/9 0009.
 * 好心情短信接口工具类
 */
public class SmsGMUtils {

    private static final String NAME = "FJB";
    private static final String PWD = "fjb123456";
    private static final String URL = "http://yl.mobsms.net/send/gsend.aspx";

    private static HashMap<String, String> codeMap = new HashMap<>();


    /**
     * 发送验证码
     *
     * @param phone 电话号码
     * @return
     * @throws Exception
     */
    public static Boolean sendCode(String phone) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        //6位随机数
        String code = RandomUtils.randomString(6);
        String codeStr = "您的本次验证码为：" + code + "。请不要把验证码泄露给其他人，若非本人操作请忽略。";

        // 创建StringBuffer对象用来操作字符串
        StringBuilder sb = new StringBuilder(URL);
        sb.append("?name=").append(NAME);
        sb.append("&pwd=").append(PWD);
        sb.append("&dst=").append(phone);
        sb.append("&msg=").append(URLEncoder.encode(codeStr, "GB2312"));

        HttpPost httpPost = new HttpPost(sb.toString());
        HttpResponse response = httpClient.execute(httpPost);

        // 执行结果
        String result = EntityUtils.toString(response.getEntity());
        if (result.contains("num=0")) {
            return false;
        } else {
            codeMap.put(phone, code);
            return true;
        }
    }

    /**
     * 校验验证码
     *
     * @param phone 手机号码
     * @param code  验证码
     * @return true表示成功，false表示失败
     * @throws Exception
     */
    public static Boolean checkCode(String phone, String code) throws Exception {
        System.out.println(codeMap);
        String resultCode = codeMap.get(phone);
        if (resultCode == null || resultCode.equals("") || !resultCode.equals(code)) {
            return false;
        } else {
            codeMap.remove(phone);
            return true;
        }

    }

    /**
     * 发送模板
     *
     * @param phone 电话号码
     * @return
     * @throws Exception
     */
    public static Boolean sendTemplet(String phone, SmsTempletEnum stEnum) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        //6位随机数
        String code = RandomUtils.randomString(6);

        // 创建StringBuffer对象用来操作字符串
        StringBuilder sb = new StringBuilder(URL);
        sb.append("?name=").append(NAME);
        sb.append("&pwd=").append(PWD);
        sb.append("&dst=").append(phone);
        sb.append("&msg=").append(URLEncoder.encode(stEnum.getTypeName(), "GB2312"));
        System.out.println(sb.toString());
        HttpPost httpPost = new HttpPost(sb.toString());
        HttpResponse response = httpClient.execute(httpPost);

        // 执行结果
        String result = EntityUtils.toString(response.getEntity());
        if (result.contains("num=0")) {
            return false;
        } else {
            codeMap.put(phone, code);
            return true;
        }
    }

    public static void main(String args[]) throws Exception {
        /*sendCode("18757550469");
        Boolean aBoolean = checkCode("18757550469", "123456");
        System.out.println(aBoolean);
        System.out.println(codeMap);*/

        //sendTemplet("18757550469", SmsTempletEnum.LOOKSMS);
    }
}
